package cn.lutu.lutu.controller;

import cn.lutu.lutu.mapper.ContentMapper;
import cn.lutu.lutu.pojo.dto.ContentCollectDTO;
import cn.lutu.lutu.pojo.dto.ContentPostDTO;
import cn.lutu.lutu.pojo.dto.ContentUpDTO;
import cn.lutu.lutu.pojo.vo.ContentListVO;
import cn.lutu.lutu.pojo.vo.ContentUpVO;
import cn.lutu.lutu.response.ResultVO;
import cn.lutu.lutu.response.StatusCode;
import cn.lutu.lutu.security.CustomUserDetails;
import cn.lutu.lutu.service.ContentService;
import cn.lutu.lutu.service.UploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@Slf4j
@Api(tags = "文章模块")
@RestController
@RequestMapping("/v1/contents/")
public class ContentController {
    @Autowired
    ContentMapper mapper;

    @Autowired
    ContentService service;

    @ApiOperation("1.发表文章")
    @PostMapping("post")
    public ResultVO post(@RequestBody ContentPostDTO contentPostDTO,@AuthenticationPrincipal CustomUserDetails cud){
//        if(cud==null){
//            return new ResultVO(StatusCode.NOT_LOGIN);
//        }
        log.info("contentPostDTO = " + contentPostDTO + ", cud = " + cud);
        service.insert(contentPostDTO, cud);
        return ResultVO.ok();
    }

    @ApiOperation("1.删除文章")
    @PostMapping("delete")
    public ResultVO deleteById(Long id){
        mapper.deleteById(id);
        return ResultVO.ok();
    }

    @ApiOperation("2.获取文章列表")
    @GetMapping("list")
    public List<ContentListVO> select(){
        return mapper.select();
    }

    @ApiOperation("3.点赞请求")
    @PostMapping("up")
    public ResultVO up(@RequestBody ContentUpDTO contentUpDTO,@AuthenticationPrincipal CustomUserDetails cud){
        if(SecurityContextHolder.getContext().getAuthentication()==null){
            return new ResultVO(StatusCode.NOT_LOGIN);
        }
        mapper.updateById(contentUpDTO.getId());
        return ResultVO.ok();
    }

    @ApiOperation("4.获取点赞数")
    @GetMapping("up_count")
    public ResultVO up_count(Integer id){
        ContentUpVO content = mapper.selectById(id);
        return ResultVO.ok(content);
    }

    @ApiOperation("5.收藏文章")
    @PostMapping("collect")
    public ResultVO collect(@RequestBody ContentCollectDTO contentCollectDTO){
        service.collect(contentCollectDTO);
        return ResultVO.ok();
    }

    @ApiOperation("6.获取收藏数")
    @GetMapping("collect_count")
    public ResultVO collectCount(Long id){
       return ResultVO.ok(mapper.selectCount(id));
    }


}
